//
//  CJCycleImagesView.m
//  AppCommonUICollect
//
//  Created by ciyouzen on 2019/10/22.
//  Copyright © 2019 dvlproad. All rights reserved.
//

#import "CJCycleImagesView.h"
#import <Masonry/Masonry.h>
#import <CJBaseUIKit/UIColor+CJHex.h>
#import <SDCycleScrollView/SDCycleScrollView.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import "CJPageControl.h"

@interface CJCycleImagesView () <SDCycleScrollViewDelegate> {
    
}
@property (nonatomic, strong) SDCycleScrollView *cycleScrollView;
@property (nonatomic, strong) CJPageControl *pageControl;

@property (nonatomic, strong, readonly) UIImage *placeholderImage;  /**< 默认占位图 */
@property (nonatomic, copy) void(^clickItemBlock)(NSInteger index);  /**< 点击Item的回调 */

@end

@implementation CJCycleImagesView

/*
*  初始化
*
*  @param placeholderImage      默认占位图
*  @param clickItemBlock        点击Item的回调
*
*  @return 循环的图片视图
*/
- (instancetype)initWithPlaceholderImage:(nullable UIImage *)placeholderImage
                          clickItemBlock:(void(^ _Nullable)(NSInteger index))clickItemBlock
{
    self = [super initWithFrame:CGRectZero];
    if (self) {
        _placeholderImage = placeholderImage;
        _clickItemBlock = clickItemBlock;
        self.scrollViewBottomlMargin = 0;
        [self setupViews];
    }
    return self;
}

#pragma mark - Setter
- (void)setInfiniteLoop:(BOOL)infiniteLoop {
    _infiniteLoop = infiniteLoop;
    self.cycleScrollView.infiniteLoop = infiniteLoop;
}

- (void)setAutoScroll:(BOOL)autoScroll {
    _autoScroll = autoScroll;
    self.cycleScrollView.autoScroll = YES;
}

- (void)setAutoScrollTimeInterval:(CGFloat)autoScrollTimeInterval {
    _autoScrollTimeInterval = autoScrollTimeInterval;
    self.autoScrollTimeInterval = autoScrollTimeInterval;
}

- (void)setShowPageControl:(BOOL)showPageControl {
    _showPageControl = showPageControl;
    self.pageControl.hidden = !showPageControl;
}


#pragma mark - Event
/**
 *  设置UI中的各种内容(使用本地图片)
 *
 *  @param imageNames           本地图片名称数组
 */
- (void)setupUIWithImageNames:(NSArray<NSString *> *)imageNames {
    self.cycleScrollView.localizationImageNamesGroup = imageNames;
    self.pageControl.numberOfPages = imageNames.count;
}

/**
 *  设置UI中的各种内容(使用网络图片)
 *
 *  @param imagesURLStrings     网络图片地址数组
 */
- (void)setupUIWithImagesURLStrings:(NSArray<NSString *> *)imagesURLStrings {
    self.pageControl.numberOfPages = imagesURLStrings.count;
    self.cycleScrollView.imageURLStringsGroup = imagesURLStrings;
}


#pragma mark - SetupViews & Lazy
- (void)setupViews {
    CJPageControl *pageControl = [[CJPageControl alloc]initWithFrame:CGRectZero];
    pageControl.pageIndicatorTintColor = CJColorFromHexString(@"#707070");
    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    pageControl.numberOfPages = 3;
    pageControl.currentPage = 0;
    [self addSubview:pageControl];
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.centerX.mas_equalTo(self);
        make.height.mas_equalTo(10);
    }];
    self.pageControl = pageControl;
    
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:self.placeholderImage];
//    cycleScrollView.placeholderImage = [UIImage imageNamed:@"produceDescriptions.png"];
    cycleScrollView.backgroundColor = [UIColor clearColor];
    cycleScrollView.autoScroll = NO;
    cycleScrollView.infiniteLoop = NO;
    cycleScrollView.showPageControl = NO;
//    cycleScrollView.pageControlBottomOffset = 10;
    [self addSubview:cycleScrollView];
    [cycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        //make.bottom.mas_equalTo(pageControl.mas_top).mas_offset(-30);
        //make.bottom.mas_equalTo(self).mas_offset(-40);
        make.bottom.mas_equalTo(self).mas_offset(self.scrollViewBottomlMargin);
        make.left.right.mas_equalTo(self);
    }];
    self.cycleScrollView = cycleScrollView;
    
    [self bringSubviewToFront:self.pageControl];
}

#pragma mark - SDCycleScrollViewDelegate
/** 图片滚动回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView
       didScrollToIndex:(NSInteger)index
{
    self.pageControl.currentPage = index;
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView
   didSelectItemAtIndex:(NSInteger)index
{
    if (self.clickItemBlock) {
        self.clickItemBlock(index);
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
